home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Multimedia & Desktop / sk8 / SK8InJava / Code / SK8 Project.java < prev   
Encoding:
Java Source  |  1997-02-27  |  16.5 KB  |  572 lines  |  [TEXT/CWIE]

  1. /*  SK8 © 1997 Apple Computer, Inc.
  2.     This code is protected under the current SK8 License
  3.     See http://sk8.research.apple.com/ for more information
  4.     Apple Research Laboratories
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.*;
  9. import java.util.Random;
  10.  
  11. // Here we store all the default instances of objects we want around.
  12. // Note that you can remove the static instances that you do not need
  13. // in order to improve startup time and memory footprint.
  14.  
  15. class sk8 {
  16.  
  17.     //----------------------------------------
  18.     //Object Instances
  19.     //----------------------------------------
  20.     private static Random random_generator = new Random();
  21.  
  22.  
  23.     
  24.     //-----------------------------------------------------------------------------------
  25.     // BEGIN GRAPHIC RELATED GLOBALS 
  26.     //   Comment these out if you wish to have a non graphical version of this code
  27.     //--------------------------------
  28.     //GLOBALS FILLED IN AT STARTUP    
  29.     public static Applet currentApplet = null;
  30.     public static Event latestEvent = null;
  31.  
  32.     //Devices
  33.     public static Mouse mouse = new Mouse();
  34.     
  35.     // The Stage
  36.     public static stage stage = null;
  37.     
  38.     //RGBColors
  39.     public static rgbcolor white = new rgbcolor(255, 255, 255);
  40.     public static rgbcolor graytone95 = new rgbcolor(243, 243, 243);
  41.     public static rgbcolor graytone90 = new rgbcolor(230, 230, 230);
  42.     public static rgbcolor graytone85 = new rgbcolor(218, 218, 218);
  43.     public static rgbcolor graytone80 = new rgbcolor(205, 205, 205);
  44.     public static rgbcolor graytone75 = new rgbcolor(192, 192, 192);
  45.     public static rgbcolor graytone70 = new rgbcolor(179, 179, 179);
  46.     public static rgbcolor graytone65 = new rgbcolor(166, 166, 166);
  47.     public static rgbcolor graytone60 = new rgbcolor(154, 154, 154);
  48.     public static rgbcolor graytone55 = new rgbcolor(141, 141, 141);
  49.     public static rgbcolor graytone50 = new rgbcolor(128, 128, 128);
  50.     public static rgbcolor graytone45 = new rgbcolor(115, 115, 115);
  51.     public static rgbcolor graytone40 = new rgbcolor(102, 102, 102);
  52.     public static rgbcolor graytone35 = new rgbcolor(90, 90, 90);
  53.     public static rgbcolor graytone30 = new rgbcolor(77, 77, 77);
  54.     public static rgbcolor graytone25 = new rgbcolor(64, 64, 64);
  55.     public static rgbcolor graytone20 = new rgbcolor(51, 51, 51);
  56.     public static rgbcolor graytone15 = new rgbcolor(38, 38, 38);
  57.     public static rgbcolor graytone10 = new rgbcolor(26, 26, 26);
  58.     public static rgbcolor graytone05 = new rgbcolor(13, 13, 13);
  59.     public static rgbcolor black = new rgbcolor(0, 0, 0);
  60.     public static rgbcolor darkgray = new rgbcolor(64, 64, 64);
  61.     public static rgbcolor gray = new rgbcolor(128, 128, 128);
  62.     public static rgbcolor lightgray = new rgbcolor(195, 195, 195);
  63.     public static rgbcolor lightbrown = new rgbcolor(223, 131, 34);
  64.     public static rgbcolor brown = new rgbcolor(180, 105, 27);
  65.     public static rgbcolor darkbrown = new rgbcolor(100, 59, 16);
  66.     public static rgbcolor lightpurple = new rgbcolor(125, 0, 255);
  67.     public static rgbcolor purple = new rgbcolor(70, 0, 164);
  68.     public static rgbcolor darkpurple = new rgbcolor(57, 0, 133);
  69.     public static rgbcolor lightblue = new rgbcolor(0, 172, 255);
  70.     public static rgbcolor blue = new rgbcolor(0, 0, 215);
  71.     public static rgbcolor darkblue = new rgbcolor(0, 0, 128);
  72.     public static rgbcolor lightgreen = new rgbcolor(59, 215, 86);
  73.     public static rgbcolor green = new rgbcolor(31, 184, 20);
  74.     public static rgbcolor darkgreen = new rgbcolor(0, 102, 17);
  75.     public static rgbcolor lightyellow = new rgbcolor(255, 255, 128);
  76.     public static rgbcolor yellow = new rgbcolor(255, 255, 0);
  77.     public static rgbcolor darkyellow = new rgbcolor(223, 223, 0);
  78.     public static rgbcolor lightorange = new rgbcolor(255, 172, 0);
  79.     public static rgbcolor orange = new rgbcolor(255, 128, 0);
  80.     public static rgbcolor darkorange = new rgbcolor(195, 98, 0);
  81.     public static rgbcolor lightred = new rgbcolor(255, 128, 128);
  82.     public static rgbcolor red = new rgbcolor(255, 0, 0);
  83.     public static rgbcolor darkred = new rgbcolor(128, 0, 0);
  84.     public static rgbcolor cyan = new rgbcolor(0, 255, 255);
  85.     public static rgbcolor tan = new rgbcolor(145, 113, 59);
  86.     public static rgbcolor pink = new rgbcolor(255, 8, 128);
  87.     public static rgbcolor vomit = new rgbcolor(207, 94, 66);
  88.     
  89.     //Gradients
  90.     public static gradient bluetunnel = new gradient(0, 128, 255, 0, 0, 62, gradient.rect);
  91.     public static gradient blacktored = new gradient(0, 0, 0, 255, 0, 0, gradient.horizontal);
  92.     public static gradient starburst = new gradient(0, 0, 0, 255, 0, 0, gradient.oval);
  93.     public static gradient metal2 = new gradient(234, 234, 234, 78, 78, 78, gradient.vertical);
  94.     public static gradient metal1 = new gradient(78, 78, 78, 234, 234, 234, gradient.vertical);
  95.     public static gradient sunset = new gradient(78, 0, 117, 255, 128, 128, gradient.vertical);
  96.     public static gradient blacktowhite = new gradient(0,0,0,255,255,255,gradient.horizontal);
  97.     
  98.     //Bevels
  99.     public static bevelrenderer uirectangleinbevel = new bevelrenderer(black, darkgray, gray, gray);
  100.     public static bevelrenderer uirectangleoutbevel = new bevelrenderer(gray, gray, black, darkgray);
  101.     
  102.     //transparent FIX ME 
  103.     public static rgbcolor transparent = sk8.white;
  104.  
  105.     
  106.     
  107.     //--------------------------------
  108.     // END GRAPHIC RELATED GLOBALS 
  109.     //-----------------------------------------------------------------------------------
  110.  
  111.     //symbol
  112.     public static symbol symbol = sk8.quote("SYMBOL");
  113.     
  114.     //Constants
  115.     public static double pi = Math.PI;
  116.     public static double e = Math.E;
  117.     
  118.     //----------------------------------------
  119.     //Project Functions
  120.     //----------------------------------------
  121.  
  122.     public static Object sk8new (Object obj) {
  123.         try {
  124.             try {
  125.                 Class objclass = obj.getClass();
  126.                 Object newobject = objclass.newInstance();
  127.                 return newobject;
  128.             } catch (InstantiationException e) {
  129.                 return null;
  130.             }
  131.         } catch (IllegalAccessException e) {
  132.             return null;
  133.         }
  134.     }
  135.  
  136.  
  137.     //SendToLog
  138.     public static void sendtolog (Object obj) {
  139.         System.out.println(obj);
  140.     }
  141.     public static void sendtolog (int t) {
  142.         System.out.println(t);
  143.     }
  144.     
  145.     //Random Numbers
  146.     public static int random(int limit) {
  147.         if (limit == 0) {
  148.             return 0;
  149.         } else {
  150.             int res = (random_generator.nextInt() % limit);
  151.             res = Math.abs(res);
  152.             return res;
  153.         }
  154.     }
  155.     public static float random(float limit) {
  156.         if (limit == 0) {
  157.             return 0;
  158.         } else {
  159.             float res = (random_generator.nextFloat() % limit);
  160.             return res;
  161.         }
  162.     }
  163.     
  164.     
  165.     //Symbols
  166.     public static symbol quote (String nameForSym) {  //sk8 function wrapper for symbol static method getsymbol
  167.         return symbol.getsymbol(nameForSym.toUpperCase());
  168.     }
  169.  
  170.  
  171.     public static list keys (table t) {
  172.         return t.keys();
  173.     }
  174.     
  175.     //Lists
  176.     public static list list () {
  177.         return new list();
  178.     }
  179.     
  180.     public static list list (Object obj1) {
  181.         list ll = new list();
  182.         ll.push(obj1);
  183.         return ll;
  184.     }
  185.     public static list list (Object obj1, Object obj2) {
  186.         list ll = new list();
  187.         ll.push(obj2);
  188.         ll.push(obj1);
  189.         return ll;
  190.     }
  191.     public static list list (Object obj1, Object obj2, Object obj3) {
  192.         list ll = new list();
  193.         ll.push(obj3);
  194.         ll.push(obj2);
  195.         ll.push(obj1);
  196.         return ll;
  197.     }
  198.     public static list list (Object obj1, Object obj2, Object obj3, Object obj4) {
  199.         list ll = new list();
  200.         ll.push(obj4);
  201.         ll.push(obj3);
  202.         ll.push(obj2);
  203.         ll.push(obj1);
  204.         return ll;
  205.     }
  206.     public static list list (Object obj1, Object obj2, Object obj3, Object obj4, Object obj5) {
  207.         list ll = new list();
  208.         ll.push(obj5);
  209.         ll.push(obj4);
  210.         ll.push(obj3);
  211.         ll.push(obj2);
  212.         ll.push(obj1);
  213.         return ll;
  214.     }
  215.     public static list list (Object obj1, Object obj2, Object obj3, Object obj4, Object obj5, Object obj6) {
  216.         list ll = new list();
  217.         ll.push(obj6);
  218.         ll.push(obj5);
  219.         ll.push(obj4);
  220.         ll.push(obj3);
  221.         ll.push(obj2);
  222.         ll.push(obj1);
  223.         return ll;
  224.     }
  225.     public static list list (Object obj1, Object obj2, Object obj3, Object obj4, Object obj5, Object obj6, Object obj7) {
  226.         list ll = new list();
  227.         ll.push(obj7);
  228.         ll.push(obj6);
  229.         ll.push(obj5);
  230.         ll.push(obj4);
  231.         ll.push(obj3);
  232.         ll.push(obj2);
  233.         ll.push(obj1);
  234.         return ll;
  235.     }
  236.     
  237.     public static list list (int obj1) {
  238.         list ll = new list();
  239.         ll.push(new Integer(obj1));
  240.         return ll;
  241.     }
  242.     public static list list (int obj1, int obj2) {
  243.         list ll = new list();
  244.         ll.push(new Integer(obj2));
  245.         ll.push(new Integer(obj1));
  246.         return ll;
  247.     }
  248.     public static list list (int obj1, int obj2, int obj3) {
  249.         list ll = new list();
  250.         ll.push(new Integer(obj3));
  251.         ll.push(new Integer(obj2));
  252.         ll.push(new Integer(obj1));
  253.         return ll;
  254.     }
  255.     public static list list (int obj1, int obj2, int obj3, int obj4) {
  256.         list ll = new list();
  257.         ll.push(new Integer(obj4));
  258.         ll.push(new Integer(obj3));
  259.         ll.push(new Integer(obj2));
  260.         ll.push(new Integer(obj1));
  261.         return ll;
  262.     }
  263.     public static list list (float obj1) {
  264.         list ll = new list();
  265.         ll.push(new Float(obj1));
  266.         return ll;
  267.     }
  268.     public static list list (float obj1, float obj2) {
  269.         list ll = new list();
  270.         ll.push(new Float(obj2));
  271.         ll.push(new Float(obj1));
  272.         return ll;
  273.     }
  274.     public static list list (float obj1, float obj2, float obj3) {
  275.         list ll = new list();
  276.         ll.push(new Float(obj3));
  277.         ll.push(new Float(obj2));
  278.         ll.push(new Float(obj1));
  279.         return ll;
  280.     }
  281.     public static list list (float obj1, float obj2, float obj3, float obj4) {
  282.         list ll = new list();
  283.         ll.push(new Float(obj4));
  284.         ll.push(new Float(obj3));
  285.         ll.push(new Float(obj2));
  286.         ll.push(new Float(obj1));
  287.         return ll;
  288.     }
  289.  
  290.  
  291.     //To make a list of red, green and blue, you make a static array of these guys
  292.     //and call the sk8.list function.  e.g.: 
  293.     //Object tempvar[] = {sk8.red, sk8.green, sk8.blue};
  294.     //sk8.list(tempvar);
  295.     
  296.     public static list list (Object elements[]) {
  297.         list ll = new list();
  298.         for (int i = elements.length - 1; i >= 0; i--) {
  299.             ll.push(elements[i]);
  300.         }
  301.         return ll;
  302.     }
  303.     
  304.     public static list list (int elements[]) {
  305.         list ll = new list();
  306.         for (int i = elements.length - 1; i >= 0; i--) {
  307.             ll.push(new Integer(elements[i]));
  308.         }
  309.         return ll;
  310.     }
  311.     public static list list (float elements[]) {
  312.         list ll = new list();
  313.         for (int i = elements.length - 1; i >= 0; i--) {
  314.             ll.push(new Float(elements[i]));
  315.         }
  316.         return ll;
  317.     }
  318.  
  319.  
  320.     //-----------------------------------------------------------------------------------
  321.     //Coercion
  322.     //-----------------------------------------------------------------------------------
  323.     
  324.     public static symbol asa (String str, symbol s) {
  325.         return sk8.quote(str);
  326.     }
  327.  
  328.     public static String asa (Object obj, String s) {
  329.         return obj.toString();
  330.     }
  331.  
  332.     
  333.     //-----------------------------------------------------------------------------------
  334.     //Collection Front End
  335.     //-----------------------------------------------------------------------------------
  336.  
  337.     public static void insertinfront (Object it, collection ll) {
  338.         ll.push(it);
  339.     }
  340.     public static void insertatend (Object it, collection ll) {
  341.         ll.pushend(it);
  342.     }
  343.     public static void insertinfront (int it, collection ll) {
  344.         ll.push(new Integer(it));
  345.     }
  346.     public static void insertatend (int it, collection ll) {
  347.         ll.pushend(new Integer(it));
  348.     }
  349.     public static void insertinfront (float it, collection ll) {
  350.         ll.push(new Float(it));
  351.     }
  352.     public static void insertatend (float it, collection ll) {
  353.         ll.pushend(new Float(it));
  354.     }
  355.     public static void insertinfront (char it, collection ll) {
  356.         ll.push(new Character(it));
  357.     }
  358.     public static void insertatend (char it, collection ll) {
  359.         ll.pushend(new Character(it));
  360.     }
  361.     
  362.     public static String insertinfront (String it, String ll) {
  363.         return it.concat(ll);
  364.     }
  365.     public static String insertatend (String it, String ll) {
  366.         return ll.concat(it);
  367.     }
  368.     
  369.     public static collection reverse (list ll) {
  370.         return ll.reverse();
  371.     }
  372.     
  373.     public static String reverse (String ll) {
  374.         StringBuffer s1 = new StringBuffer(ll);
  375.         return s1.reverse().toString();
  376.     }
  377.     
  378.     public static int length (collection ll) {
  379.         return ll.length();
  380.     }
  381.     
  382.     public static int length (String ll) {
  383.         return ll.length();
  384.     }
  385.     
  386.     public static boolean contains (collection ll, Object obj) {
  387.         return ll.contains(obj);
  388.     }
  389.     
  390.     public static boolean contains (collection ll, char ch) {
  391.         Character c = new Character(ch);
  392.         return ll.contains((Object)c);
  393.     } 
  394.     
  395.     public static list concatenate (list ll1, list ll2) {
  396.         return list.concatenate(ll1,ll2);
  397.     }
  398.     public static list concatenate (list ll1, Object e) {
  399.         ll1.pushend(e);
  400.         return ll1;
  401.     }
  402.     public static String concatenate (String str1, String str2) {
  403.         return (str1 + str2);
  404.     }
  405.     
  406.     
  407.     public static list collectionsubsequence(collection c, int start, int end) {
  408.         return c.range(start, end);
  409.     }
  410.     
  411.     public static String collectionsubsequence(String c, int start, int end) {
  412.         return c.substring(start - 1, end);
  413.     }
  414.     
  415.  
  416.     //-----------------------------------------------------------------------------------
  417.     //Math functions
  418.     //-----------------------------------------------------------------------------------
  419.  
  420.     public static int max (int i1, int i2) {
  421.         return Math.max(i1, i2);
  422.     }
  423.     public static float max (float i1, float i2) {
  424.         return Math.max(i1, i2);
  425.     }
  426.     public static double max (double i1, double i2) {
  427.         return Math.max(i1, i2);
  428.     }
  429.     public static long max (long i1, long i2) {
  430.         return Math.max(i1, i2);
  431.     }
  432.     public static int min (int i1, int i2) {
  433.         return Math.min(i1, i2);
  434.     }
  435.     public static float min (float i1, float i2) {
  436.         return Math.min(i1, i2);
  437.     }
  438.     public static double min (double i1, double i2) {
  439.         return Math.min(i1, i2);
  440.     }
  441.     public static long min (long i1, long i2) {
  442.         return Math.min(i1, i2);
  443.     }
  444.     public static int abs (int i1) {
  445.         return Math.abs(i1);
  446.     }
  447.     public static float abs (float i1) {
  448.         return Math.abs(i1);
  449.     }
  450.     public static double abs (double i1) {
  451.         return Math.abs(i1);
  452.     }
  453.     public static long abs (long i1) {
  454.         return Math.abs(i1);
  455.     }
  456.     public static double floor (double i1) {
  457.         return Math.floor(i1);
  458.     }
  459.     public static double ceiling (double i1) {
  460.         return Math.ceil(i1);
  461.     }
  462.     public static double sqrt (double i1) {
  463.         return Math.sqrt(i1);
  464.     }
  465.     public static double log (double i1) {
  466.         return Math.log(i1);
  467.     }
  468.     public static double exp (double i1) {
  469.         return Math.exp(i1);
  470.     }
  471.     public static int round (float i1) {
  472.         return Math.round(i1);
  473.     }
  474.     
  475.  
  476.     //-----------------------------------------------------------------------------------
  477.     // BEGIN GRAPHIC RELATED FUNCTIONS 
  478.     //   Comment these out if you wish to have a non graphical version of this code
  479.     //--------------------------------
  480.  
  481.     //-----------------------------------------------------------------------------------
  482.     //  Event Related Functions
  483.     //-----------------------------------------------------------------------------------
  484.     public static actor eventactor () {
  485.         return sk8.stage.eventactorVar;
  486.     }
  487.     
  488.     public static actor eventwindow() {
  489.         return sk8.stage.eventwindowVar;
  490.     }
  491.     
  492.     public static long eventtime() {
  493.         return sk8.stage.eventtimeVar;
  494.     }
  495.     
  496.     public static int eventh() {
  497.         return sk8.stage.eventhVar;
  498.     }
  499.     
  500.     public static int eventv() {
  501.         return sk8.stage.eventvVar;
  502.     }
  503.  
  504.     //Modifier keys
  505.     
  506.     public static boolean shiftkeydown() {
  507.         return sk8.stage.shiftkeydownVar;
  508.     }    
  509.  
  510.     public static boolean controlkeydown() {
  511.         return sk8.stage.controlkeydownVar;
  512.     }    
  513.  
  514.     public static boolean commandkeydown() {
  515.         return sk8.stage.commandkeydownVar;
  516.     }    
  517.  
  518.     public static boolean optionkeydown() {
  519.         return sk8.stage.optionkeydownVar;
  520.     }    
  521.     
  522.     //actorathvcoordinations
  523.     //IMPLEMENT ME FIX ME
  524.     public static actor actorathvcoordinates(int h, int v) {
  525.         return null;
  526.     }
  527.     
  528.     //-----------------------------------------------------------------------------------
  529.     //  XOR Drawing Functions
  530.     //  *** Stubs for now.  Is this available in Java?
  531.     //-----------------------------------------------------------------------------------
  532.     public static void zoomrect(int l1, int t1, int r1, int b1, 
  533.                                 int l2, int t2, int r2, int b2) {
  534.                                 
  535.         //ZOOM!!! :)
  536.     }
  537.     public static void drawxorframe(int l1, int t1, int r1, int b1) {
  538.         //Whoosh!!
  539.     }
  540.     
  541.     
  542.     //-----------------------------------------------------------------------------------
  543.     // Interactions with the user...
  544.     //-----------------------------------------------------------------------------------
  545.     
  546.     public static void messagetouser (String message) {
  547.     /* @@@@@@FIX ME   Need to figure out how to get frame for this.
  548.         messagedialog thedialog = new messagedialog(FRAME GOES HERE, message);
  549.         list monitorcenter = sk8.mainmonitorcenter();
  550.         Dimension mysize = thedialog.size();
  551.         thedialog.move(monitorcenter.nthint(1) - mysize.width/2, 
  552.                         monitorcenter.nthint(2) - mysize.height/2);
  553.         thedialog.show();
  554.         */
  555.     }
  556.     
  557.     //-----------------------------------------------------------------------------------
  558.     // system info...
  559.     //-----------------------------------------------------------------------------------
  560.     
  561.     public static list mainmonitorcenter () {
  562.         Rectangle bounds = sk8.stage.boundsrect();
  563.         return sk8.list(bounds.x + bounds.width/2, bounds.y + bounds.height/2);
  564.     }
  565.     
  566.     //--------------------------------
  567.     // END GRAPHIC RELATED FUNCTIONS 
  568.     //-----------------------------------------------------------------------------------
  569.     
  570.     
  571.     
  572.     }